import { loadSprite } from '../helper/Loader'
const { ccclass, property } = cc._decorator
/**
 * @description 游戏状态枚举,0开始游戏，1游戏通过，2游戏失败
 * @enum {number}
 */
export enum PlayStatus {
  Start,
  Fail,
  Pause
}
interface EventMap {
  [key: number]: { string: string, callback: Function }
}

@ccclass
export default class ButtonDialog extends cc.Component {
  private finalButton: cc.Node = null // 
  private eventMap: EventMap = {} // 事件列表
  private status: PlayStatus = 0 // 游戏状态
  private transferData: any = null
  onLoad() {
    this.finalButton = new cc.Node('finalButton')
    this.finalButton.setContentSize(cc.size(200, 150))
    this.node.addChild(this.finalButton)
    if (CC_DEBUG) {
      const coll = this.finalButton.addComponent(cc.BoxCollider)
      coll.size = cc.size(this.finalButton.width, this.finalButton.height)
    }
  }
  start() {
    this.render()
  }
  /**
   * @description 修改节点状态
   * @author 刘建文
   * @date 2019-10-17
   * @param {PlayStatus} status
   * @memberof ButtonDialog
   */
  public SetStatus(status: PlayStatus, transferData) {
    this.finalButton.active = true
    this.status = status
    this.transferData = transferData
    this.render()
  }
  /**
   * @description 注册事件
   * @author 刘建文
   * @date 2019-10-17
   * @param {EventMap} eventMap
   * @memberof ButtonDialog
   */
  public RegisterEvent(eventMap: EventMap) {
    this.eventMap = eventMap
  }
  /**
   * @description 节点渲染
   * @author 刘建文
   * @date 2019-10-17
   * @private
   * @memberof ButtonDialog
   */
  private async render() {
    this.finalButton.removeAllChildren()
    const backNode = new cc.Node('backNode')
    backNode.setContentSize(cc.size(this.finalButton.width, this.finalButton.height))
    this.finalButton.addChild(backNode)
    // 加载弹窗背景图
    const Texture2D = await loadSprite('image/Common', cc.Texture2D)
    let sp = backNode.addComponent(cc.Sprite)
    const spriteFrame = new cc.SpriteFrame(Texture2D, new cc.Rect(270, 125, 114, 85), false, cc.Vec2.ZERO, cc.size(100, 100))
    sp.spriteFrame = spriteFrame
    backNode.scaleX = this.finalButton.width / backNode.width
    backNode.scaleY = this.finalButton.height / backNode.height
    if (CC_DEBUG) {
      const coll = backNode.addComponent(cc.BoxCollider)
      coll.size = cc.size(backNode.width, backNode.height)
    }
    if (this.transferData) {
      const transferNode = new cc.Node()
      this.finalButton.addChild(transferNode)
      let extra = transferNode.addComponent(cc.Label)
      extra.string = this.transferData
      extra.fontSize = 14
      transferNode.position = cc.v2(0, this.finalButton.height / 4)
    }
    // 添加按钮节点
    const btnNode = new cc.Node('button')
    this.finalButton.addChild(btnNode)
    const btn = btnNode.addComponent(cc.Button)
    // 按钮事件
    btnNode.on(cc.Node.EventType.TOUCH_START, (e: cc.Component.EventHandler) => {
      this.finalButton.active = false
      this.eventMap[this.status].callback()
    })
    // 添加文本节点
    // 不添加到backnode中，防止拉伸导致字体变形
    // 先添加文本再添加背景，以便获取文本节点宽高，用于设置背景图
    const labelNode = new cc.Node('label')
    labelNode.height = 10
    btnNode.addChild(labelNode)
    const label = labelNode.addComponent(cc.Label)
    label.fontSize = 14
    label.lineHeight = 14
    label.string = this.eventMap[this.status].string
    // 添加按钮背景图
    const btnSprite = btnNode.addComponent(cc.Sprite)
    btn.normalSprite = await loadSprite('image/btn_normal', cc.SpriteFrame)
    btnSprite.spriteFrame = btn.normalSprite
    btnSprite.type = cc.Sprite.Type.SLICED
    // 先更新label填充之后的状态，再设置按钮节点的宽高
    label._updateRenderData(true)
    btnNode.setContentSize(cc.size(labelNode.width + 10, labelNode.height + 10))
  }
}
